﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Vehicle.Web.Pages.Role
{
    /// <summary>
    /// Copyright (C) M-Tear团队
    /// 文 件 名：Role_Edit.aspx.cs
    /// 版    本：v1.00.0000
    /// 创建标识：2009-08-27   Created by 杨光远 V1.00.0000
    /// 功能说明：角色编辑页面
    /// 注意事项：无
    /// 
    /// 更新记录：
    /// 
    /// </summary>
    public partial class Row_Edit : Vehicle.Web.Component.PageBase
    {
        #region---基本变量的实例化---
        BLL.Log b_Log = new Vehicle.BLL.Log();
        BLL.Role b_Role = new BLL.Role();
        #endregion

        #region---基本操作---
        /// <summary>
        /// 页面加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                UserDataBind();
            }
        }

        /// <summary>
        /// 用户数据绑定
        /// </summary>
        public void UserDataBind()
        {
            if (Request.QueryString["id"] != null)
            {
                try
                {
                    //根据角色编号查询角色信息返回内存中的一张表
                    DataTable dt = b_Role.GetModel(new Model.ComponentsInfo("Role", "R_ID", Request.QueryString["id"].ToString(), "R_ID"));
                    Txt_R_Name.Text = dt.Rows[0]["R_Name"].ToString();//角色名称
                    Txt_R_Note.Text = dt.Rows[0]["R_Note"].ToString();//角色说明
                    hf_R_Name.Value = dt.Rows[0]["R_Name"].ToString();//角色名称隐藏值
                }
                catch
                {
                    Vehicle.Common.MessageHelper.AlertBack("操作错误!");
                }
            }
            else
            {
                Vehicle.Common.MessageHelper.AlertBack("操作错误!");
            }
        }
        /// <summary>
        /// 修改角色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Btn_Go_Click(object sender, EventArgs e)
        {
            if (hf_R_Name.Value != Txt_R_Name.Text.Trim())//判断是否有相同的角色
            {
                if (b_Role.GetModel(new Vehicle.Model.ComponentsInfo("Role", "R_Name", this.Txt_R_Name.Text.Trim(), "R_ID")).Rows.Count == 0)
                {
                    RoleUpdate();
                }
                else
                {
                    Vehicle.Common.MessageHelper.Alert("系统已经存在相同的角色,请重新填写");
                }
            }
            else
            {
                RoleUpdate();
            }
        }
        /// <summary>
        /// 更新角色
        /// </summary>
        protected void RoleUpdate()
        {
            b_Role.Update(new Model.RoleInfo(Vehicle.Common.TextHelper.StringToInt(Request.QueryString["id"].ToString()), Txt_R_Name.Text.Trim(), Vehicle.Common.TextHelper.CheckStr(Txt_R_Note.Text.Trim())));//更新角色信息
            b_Log.Add(new Vehicle.Model.LogInfo(session_ID, DateTime.Now, "成功修改角色" + Txt_R_Name.Text.Trim() + "的信息"));//添加日志信息
            Vehicle.Common.MessageHelper.AlertRedirect("修改成功!", "Main.aspx");

        }
        #endregion
    }
}
